﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace IronBoxControls.src.IronBox.Core.Controls
{
    /// <summary>
    /// 铁盒风格按钮控件，支持图标、加载状态和主题切换
    /// </summary>
    public class IronButton : IronBoxControlBase
    {
        #region 依赖属性：按钮文本
        /// <summary>
        /// 按钮显示的文本
        /// </summary>
        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }

        public static readonly DependencyProperty TextProperty =
            RegisterDependencyProperty<string>(nameof(Text), string.Empty);
        #endregion

        #region 依赖属性：是否加载中
        /// <summary>
        /// 是否显示加载状态（加载时禁用点击）
        /// </summary>
        public bool IsLoading
        {
            get => (bool)GetValue(IsLoadingProperty);
            set => SetValue(IsLoadingProperty, value);
        }

        public static readonly DependencyProperty IsLoadingProperty =
            RegisterDependencyProperty<bool>(
                nameof(IsLoading),
                false,
                OnIsLoadingChanged);  // 状态变化时触发回调

        private static void OnIsLoadingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is IronButton button && e.NewValue is bool isLoading)
            {
                // 关键修改：将 "Normal" 改为 "NotLoading"，与 XAML 中的状态名称对应
                button.GoToVisualState(isLoading ? "Loading" : "NotLoading");
                button.IsEnabled = !isLoading;
            }
        }
        #endregion

        #region 依赖属性：点击命令
        /// <summary>
        /// 按钮点击时执行的命令（支持MVVM绑定）
        /// </summary>
        public ICommand ClickCommand
        {
            get => (ICommand)GetValue(ClickCommandProperty);
            set => SetValue(ClickCommandProperty, value);
        }

        public static readonly DependencyProperty ClickCommandProperty =
            RegisterDependencyProperty<ICommand>(nameof(ClickCommand), null);
        #endregion

        #region 构造函数与样式初始化
        static IronButton()
        {
            // 指定控件使用的默认样式（在Themes项目中定义）
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(IronButton),
                new FrameworkPropertyMetadata(typeof(IronButton)));
        }
        #endregion

        #region 点击事件处理
        /// <summary>
        /// 处理鼠标左键点击
        /// </summary>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            if (IsEnabled && !IsLoading)  // 仅在启用且非加载状态下响应
            {
                OnClick();
                e.Handled = true;  // 避免事件冒泡干扰
            }
        }

        /// <summary>
        /// 点击逻辑核心：执行命令并触发事件
        /// </summary>
        protected virtual void OnClick()
        {
            // 执行绑定的命令（支持MVVM）
            ClickCommand?.Execute(null);
            // 触发路由事件（支持传统事件绑定）
            RaiseClickEvent();
        }

        /// <summary>
        /// 定义点击路由事件
        /// </summary>
        public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
            nameof(Click),
            RoutingStrategy.Bubble,
            typeof(RoutedEventHandler),
            typeof(IronButton));

        /// <summary>
        /// 点击事件（供外部订阅）
        /// </summary>
        public event RoutedEventHandler Click
        {
            add => AddHandler(ClickEvent, value);
            remove => RemoveHandler(ClickEvent, value);
        }

        /// <summary>
        /// 触发点击事件
        /// </summary>
        private void RaiseClickEvent()
        {
            var args = new RoutedEventArgs(ClickEvent, this);
            RaiseEvent(args);
        }
        #endregion
    }
}
